/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package encryptfiles;

/**
 *
 * @author balancy
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.JOptionPane;

public class EncryptFiles {

private static final String key = "155Pm)?Owc=I83bstrLH^8zL[7\"'7;"; // needs to be at least 8 characters for DES

    public static void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                System.out.println(fileEntry.getName());
            }
        }
    }

    public static void main(String a[]) {
        //getMAC();
        /*
         final File folder = new File("toEncrp");
         //listFilesForFolder(folder);
         for (final File fileEntry : folder.listFiles()) {
         if (fileEntry.isDirectory()) {
         //listFilesForFolder(fileEntry);
         } else {
         try {
         System.out.println(fileEntry.getName());
         encrypt(new FileInputStream(folder.getAbsolutePath() + File.separator + fileEntry.getName()), new FileOutputStream(folder.getAbsolutePath()+"/b/"  + fileEntry.getName()+""));
         } catch (Throwable ex) {
         Logger.getLogger(CipherDES.class.getName()).log(Level.SEVERE, null, ex);
         }
         }
         }
        
         */

        final File folder = new File("toEncrp");
        //listFilesForFolder(folder);
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                //listFilesForFolder(fileEntry);
            } else {
                try {
                    System.out.println(fileEntry.getName());
                    //decrypt(new FileInputStream(folder.getAbsolutePath() + File.separator + fileEntry.getName()), new FileOutputStream(folder.getAbsolutePath() + "/drp/" + fileEntry.getName() + ""));
                    encrypt(new FileInputStream(folder.getAbsolutePath() + File.separator + fileEntry.getName()), new FileOutputStream(folder.getAbsolutePath() + "/ecrp/" + fileEntry.getName() + ""));
                } catch (Throwable ex) {
                    Logger.getLogger(EncryptFiles.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        JOptionPane.showMessageDialog(null, "Done");

        ///*        
        /*
         BufferedReader br = decrypt(new FileInputStream("F10[encr]"));
         String line = null;
         while ((line = br.readLine()) != null) {
         System.out.println(line);
         }
         System.out.println("|||");
         */
        ///
    }

    public static void encrypt(InputStream is, OutputStream os) throws Throwable {
        encryptOrDecrypt(Cipher.ENCRYPT_MODE, is, os);
    }

    public static void decrypt(InputStream is, OutputStream os) throws Throwable {
        encryptOrDecrypt(Cipher.DECRYPT_MODE, is, os);
    }

    public static BufferedReader decrypt(InputStream is) throws Throwable {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey desKey = skf.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        return new BufferedReader(new InputStreamReader(cis, "UTF-8"));
    }

    public static CipherInputStream decryptAnswerkey(InputStream is) throws Throwable {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey desKey = skf.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        return cis;//new InputStreamReader(cis, "UTF-8");
    }

    public static void encryptOrDecrypt(int mode, InputStream is, OutputStream os) throws Throwable {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey desKey = skf.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
        if (mode == Cipher.ENCRYPT_MODE) {
            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            CipherInputStream cis = new CipherInputStream(is, cipher);
            doCopy(cis, os);
        } else if (mode == Cipher.DECRYPT_MODE) {
            cipher.init(Cipher.DECRYPT_MODE, desKey);
            CipherOutputStream cos = new CipherOutputStream(os, cipher);
            doCopy(is, cos);
        }
    }

    public static void reader(InputStream is) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }

    public static void doCopy(InputStream is, OutputStream os) throws Exception {
        byte[] bytes = new byte[64];
        int numBytes;
        while ((numBytes = is.read(bytes)) != -1) {
            os.write(bytes, 0, numBytes);
        }
        os.flush();
        os.close();
        is.close();
    }
    
}
